class SearchController < ApplicationController 
    
    layout 'default'
    
    def initialize
        @path_limit = 50
    end
    
    #########################################################
    # Show search page if there is nothing specified to search for
    # or if there are searchable params search for them
    def index
        render
    end
    
    #########################################################
    # search for the given terms
    def search_results
        
        if params[:search]
            search_type = params[:search][:type] || 'all'
            search_terms = params[:search][:terms]
        else
            search_type = params[:type] || 'all'
            search_terms = params[:terms]
        end
        
        do_search(search_terms, search_type)
    end
        
        
    #########################################################
    # just search uris
    def uris        
        search_type = 'uri'
        search_terms = params[:uri]
        do_search(search_terms, search_type)
    end
    
    protected
    
    #########################################################
    # search all fields for the given terms
    def do_search(search_terms, search_type = 'all')
        
        if search_terms
            @terms = search_terms.collect {|term| term}
            sql_terms = search_terms.collect {|term| "%" + term + "%"}.join(' OR ').downcase

            search_conditions = 'SELECT DISTINCT ON (path_created_at, paths.id) paths.*, users.login, '
            search_conditions << 'paths.created_at AS path_created_at FROM paths ' 
            search_conditions << 'INNER JOIN steps ON steps.path_id = paths.id '
            search_conditions << 'INNER JOIN users ON users.id = paths.user_id '
            search_conditions << 'INNER JOIN links ON links.id = steps.link_id '
                 
            if search_type == 'all'
                search_conditions << 'WHERE ((lower(steps.title) LIKE ? ) '
                search_conditions << 'OR (lower(links.uri) LIKE ? ) '
                search_conditions << 'OR (lower(paths.title) LIKE ? )) ' 
                sql_term_array = [sql_terms, sql_terms, sql_terms]
                
            elsif search_type == 'path_titles'
                search_conditions << 'WHERE (lower(paths.title) LIKE ? )' 
                sql_term_array = [sql_terms]
                
            elsif search_type == 'uri'
                search_conditions << 'WHERE (lower(links.uri) LIKE ? ) '
                sql_term_array = [sql_terms]
                        
            else search_type == 'path_contents'
                search_conditions << "WHERE ((lower(steps.title) LIKE ? ) OR (lower(links.uri) LIKE ? )) " 
                sql_term_array = [sql_terms, sql_terms]
                
            end

            search_conditions << "AND paths.published = 't' ORDER BY path_created_at, paths.id DESC "
            search_conditions << 'LIMIT ' + @path_limit.to_s

            @paths = Path.find_by_sql([search_conditions] + sql_term_array)
            if @paths.length <= 0
                @page_title = "No Paths Found for: " + @terms.to_sentence
            else
                @page_title = "Paths found for: " + @terms.to_sentence
            end
            
        else
            if params[:terms].nil?
                @page_title = 'No paths found for: ' + params[:terms]
            else
                @page_title = 'If you want me to find something you have to tell me what to look for.'
            end
            @paths = []
        end
                
        self.render_results
    end

    #########################################################
    # all the results are rendered the same
    def render_search
        respond_to do |format|
            format.html { render :template => '/search/index' }
            format.xml  { render :xml => flash.to_xml }
            format.rss  { render :xml => flash.to_xml }
            format.atom  { render :xml => flash.to_xml }
            format.rdf  { render :xml => flash.to_xml }
        end
    end
        
    #########################################################
    # all the results are rendered the same
    def render_results
        respond_to do |format|
            format.html { render :template => '/search/results' }
            format.xml  { render :action => '../shared/paths_xml.rxml', :layout=>false }
            format.rss  { render :action => '../shared/paths_rss.rxml', :layout=>false }
            format.atom { render :action => '../shared/paths_atom.rxml', :layout=>false }
            format.rdf  { render :action => '../shared/paths_rdf.rxml', :layout=>false }
        end
    end
    
end
